package com.example.wanandroidsdk.http;

import com.example.wanandroidsdk.constants.Constants;
import com.example.wanandroidsdk.utils.LogUtils;

import java.util.HashMap;
import java.util.Map;

public class WanHttpRequest {
    private static final String TAG = "WanHttp";

    public static void getBanner(final WanHttpRequestCallBack callBack) {
        getHomeBanner(httpResult -> {
            if ((httpResult != null ? httpResult.getData() : null) == null) {
                return;
            }
            try {
                String content = httpResult.getData().toString();
                if (httpResult.isBizSucceed(true)) {
                    callBack.onResponse(content);
                } else {
                    String message = httpResult.getErrorMsg();
                    callBack.onFailed(message);
                }
                LogUtils.e(TAG, content);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

    public static void getHotKey(final WanHttpRequestCallBack callBack) {
        getHotKey(httpResult -> {
            if ((httpResult != null ? httpResult.getData() : null) == null) {
                return;
            }
            try {
                String content = httpResult.getData().toString();
                if (httpResult.isBizSucceed(true)) {
                    callBack.onResponse(content);
                } else {
                    String message = httpResult.getErrorMsg();
                    callBack.onFailed(message);
                }
                LogUtils.e(TAG, content);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

    public static void getTreeList(final WanHttpRequestCallBack callBack) {
        getTree(httpResult -> {
            if ((httpResult != null ? httpResult.getData() : null) == null) {
                return;
            }
            try {
                String content = httpResult.getData().toString();
                if (httpResult.isBizSucceed(true)) {
                    callBack.onResponse(content);
                } else {
                    String message = httpResult.getErrorMsg();
                    callBack.onFailed(message);
                }
                LogUtils.e(TAG, content);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

    /**
     * 获取首页banner接口
     *
     * @param responseListener
     * @return
     */
    public static WanHttpUtils.RequestThread getHomeBanner(WanOnResponseListener responseListener) {
        if (responseListener == null) {
            return null;
        }
        Map<String, String> params = new HashMap<>();
        String url = Constants.BASE_URL + Constants.BANNER_LIST;
        params.put("url", url);
        params.put("Method", "GET");
        WanHttpUtils.RequestThread requestThread = new WanHttpUtils.RequestThread(params, responseListener);
        requestThread.start();
        return requestThread;
    }

    /**
     * 获取搜索热词接口
     *
     * @param responseListener
     * @return
     */
    private static WanHttpUtils.RequestThread getHotKey(WanOnResponseListener responseListener) {
        if (responseListener == null) {
            return null;
        }
        Map<String, String> params = new HashMap<>();
        String url = Constants.BASE_URL + Constants.HOT_KEY;
        params.put("url", url);
        params.put("Method", "GET");
        WanHttpUtils.RequestThread requestThread = new WanHttpUtils.RequestThread(params, responseListener);
        requestThread.start();
        return requestThread;
    }

    private static WanHttpUtils.RequestThread getTree(WanOnResponseListener responseListener) {
        if (responseListener == null) {
            return null;
        }
        Map<String, String> params = new HashMap<>();
        String url = Constants.BASE_URL + Constants.TREE;
        params.put("url", url);
        params.put("Method", "GET");
        WanHttpUtils.RequestThread requestThread = new WanHttpUtils.RequestThread(params, responseListener);
        requestThread.start();
        return requestThread;
    }
}
